package com.person.jiaiplan.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.person.jiaiplan.pojo.dto.expenses.ExpensesFinishReqDTO;
import com.person.jiaiplan.pojo.entity.ExpensesCountLogs;
import com.person.jiaiplan.pojo.entity.ExpensesPlan;
import com.person.jiaiplan.pojo.signle.SignleSnowFlake;
import com.person.jiaiplan.service.ExpensesCountLogsService;
import com.person.jiaiplan.service.ExpensesPlanService;
import com.person.jiaiplan.service.ExpensesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class ExpensesServiceImpl implements ExpensesService {

    @Autowired
    private ExpensesPlanService expensesPlanService;

    @Autowired
    private ExpensesCountLogsService expensesCountLogsService;


    @Override
    public Boolean finishIt(ExpensesFinishReqDTO reqDTO) {
        ExpensesPlan expensesPlan = expensesPlanService.getById(reqDTO.getPlanId());
        if (expensesPlan == null) {
            return false;
        }else{
            expensesPlan.setStatus(1);
            expensesPlanService.updateById(expensesPlan);
        }

        createExpensesCountLogs(expensesPlan,reqDTO);

        return true;
    }

    @Override
    public Boolean deletePlan(String id) {
       Boolean result = expensesPlanService.removeById(id);
        // 同时删除统计的关联数据
        expensesCountLogsService.remove(Wrappers.<ExpensesCountLogs>lambdaQuery().eq(ExpensesCountLogs::getPlanId, Long.valueOf(id)));
        return result;
    }

    /**
     * 创建统计计划日志
     * @param expensesPlan
     * @param reqDTO
     */
    private void createExpensesCountLogs(ExpensesPlan expensesPlan,ExpensesFinishReqDTO reqDTO){
        // 判断一下统计绑定的planId 是否唯一
        ExpensesCountLogs expensesCountLogs = expensesCountLogsService.getOne(Wrappers.<ExpensesCountLogs>lambdaQuery().eq(ExpensesCountLogs::getPlanId,expensesPlan.getId()));
        Boolean isUpdate = false;
        if (expensesCountLogs != null){
            expensesCountLogs.setUpdateTime(DateUtil.date());
            isUpdate = true;
        }else{
            expensesCountLogs = new ExpensesCountLogs();
            expensesCountLogs.setId(SignleSnowFlake.getSnowflakeId());
            expensesCountLogs.setCreateTime(DateUtil.date());
        }

        expensesCountLogs.setPlanId(expensesPlan.getId());
        expensesCountLogs.setContent(expensesPlan.getContent());
        expensesCountLogs.setExpensesType(expensesPlan.getExpensesType());
        expensesCountLogs.setEstimatedAmount(expensesPlan.getEstimatedAmount());
        expensesCountLogs.setPlanStartTime(expensesPlan.getStartTime());
        expensesCountLogs.setPlanEndTime(expensesPlan.getEndTime());
        expensesCountLogs.setRealAmount(new BigDecimal(reqDTO.getRealAmount()));
        expensesCountLogs.setFinishTime(DateUtil.parse(reqDTO.getFinishTime(),"yyyy-MM-dd"));
        expensesCountLogs.setRemark(reqDTO.getRemark());

        if (isUpdate) {
            expensesCountLogsService.updateById(expensesCountLogs);
        }else{
            expensesCountLogsService.save(expensesCountLogs);
        }

    }


}
